package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.dafuweng.*;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.cross.CrossDataMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.stream.IntStream;

public class DaFuWengPb {

    /**
     * 活动配置下发
     */
    public static MonopolyProto.SyncMonopolyConfigRespMsg.Builder parseSyncMonopolyConfigRespMsg(DFWConfig dfwConfig, GamePlayer player) {
        String language = player.getLanguage();
        MonopolyProto.SyncMonopolyConfigRespMsg.Builder builder = MonopolyProto.SyncMonopolyConfigRespMsg.newBuilder();
        builder.addAllBuildingConfigList(parseBuildingConfigMsgList(dfwConfig, language));
        builder.addAllMapConfigList(parseMapConfigMsgList(dfwConfig, language));
        builder.addAllBossConfigList(parseBossConfigMsgList(dfwConfig, language));
        builder.addAllEventConfigList(parseEventConfigMsgList(dfwConfig, language));
        builder.setCardConfig(parseCardConfigMsg(dfwConfig));
        builder.setUnionConfig(parseUnionConfigMsg(dfwConfig));
        builder.setUserConfig(parseUserConfigMsg(dfwConfig));
        builder.setDollConfig(parseDollConfigMsg(dfwConfig));
        builder.setSkinIdList(dfwConfig.getSkinIdList());
        builder.setBattleBaseDamage(dfwConfig.getBaseDamage());
        builder.addAllRebateConfig(parseRebateConfigList(dfwConfig.getRebateConfigMap()));
        builder.setSecretConfig(parseSecretRoadConfig(dfwConfig));
        builder.setFastBattleYongBingCardLimit(dfwConfig.getFastBattleYongBingCardLimit());
        builder.setFastBattleZhuiBuCardLimit(dfwConfig.getFastBattleZhuiBuCardLimit());
        return builder;
    }

    private static Iterable<? extends MonopolyProto.RebateConfig> parseRebateConfigList(Map<Integer, DFWRebateConfig> rebateConfigMap) {
        List<MonopolyProto.RebateConfig> list = new ArrayList<>();
        for (DFWRebateConfig dfwRebateConfig : rebateConfigMap.values()) {
            list.add(parseRebateConfig(dfwRebateConfig));
        }
        return list;
    }

    private static MonopolyProto.RebateConfig parseRebateConfig(DFWRebateConfig dfwRebateConfig) {
        MonopolyProto.RebateConfig.Builder builder = MonopolyProto.RebateConfig.newBuilder();
        builder.setTimes(dfwRebateConfig.getTimes());
        builder.setRewards(dfwRebateConfig.getRewards());
        return builder.build();

    }

    private static MonopolyProto.SecretRoadConfigMsg.Builder parseSecretRoadConfig(DFWConfig dfwConfig) {
        MonopolyProto.SecretRoadConfigMsg.Builder builder = MonopolyProto.SecretRoadConfigMsg.newBuilder();
        builder.setSecretBigRewardNumWeight(dfwConfig.getSecretBigRewardNumWeight());
        builder.setSecretBigRewardPool(dfwConfig.getSecretBigRewardPool());
        builder.setSecretNormalRewardPool(dfwConfig.getSecretNormalRewardPool());
        return builder;
    }


    /**
     * 建筑配置
     */
    public static List<MonopolyProto.BuildingConfigMsg> parseBuildingConfigMsgList(DFWConfig dfwConfig, String language) {
        Map<Integer, DFWBuildingConfig> configMap = dfwConfig.getDfwBuildingConfigMap();
        List<MonopolyProto.BuildingConfigMsg> list = new ArrayList<>();
        for (DFWBuildingConfig config : configMap.values()) {
            MonopolyProto.BuildingConfigMsg.Builder builder = MonopolyProto.BuildingConfigMsg.newBuilder();
            builder.setBuildingId(config.getBuildingId());
            builder.setName(ServerLanguageMgr.getContent(config.getBuildingName(), language));
            builder.setDesc(ServerLanguageMgr.getContent(config.getBuildingDesc(), language));
            builder.setBaseEvent(config.getBaseEvent());
            builder.setIcon(config.getIcon());
            builder.setBaseEvent(config.getBaseEvent());
            builder.addAllReachEventList(parseEventIdList(config.getReachEventList()));
            builder.addAllPassEventList(parseEventIdList(config.getPassEventList()));
            builder.addAllPositionList(config.getPositionList());
            builder.setShowSkillId(config.getShowSkillId());
            builder.setCollectTask(config.getCollectTask());
            builder.setCollectRewards(config.getCollectRewards());
            list.add(builder.build());
        }
        return list;
    }

    /**
     * 地图配置
     */
    public static List<MonopolyProto.MapConfigMsg> parseMapConfigMsgList(DFWConfig dfwConfig, String language) {
        List<MonopolyProto.MapConfigMsg> list = new ArrayList<>();
        List<DFWMapConfig> configList = dfwConfig.getDfwMapConfigList();
        for (DFWMapConfig config : configList) {
            MonopolyProto.MapConfigMsg.Builder builder = MonopolyProto.MapConfigMsg.newBuilder();
            builder.setMapId(config.getMapId());
            builder.setMapName(ServerLanguageMgr.getContent(config.getMapName(), language));
            builder.setIcon(config.getIcon());
            builder.setUnlockGold(config.getUnlockGold());
            builder.setEventId(config.getSkill());
            builder.setResource(config.getResource());
            builder.setLevel(config.getLevel());
            list.add(builder.build());
        }
        return list;
    }

    /**
     * 事件配置
     */
    public static List<MonopolyProto.EventConfigMsg> parseEventConfigMsgList(DFWConfig dfwConfig, String language) {
        List<MonopolyProto.EventConfigMsg> list = new ArrayList<>();
        Map<Integer, DFWEvent> configMap = dfwConfig.getDfwEventMap();
        for (DFWEvent config : configMap.values()) {
            MonopolyProto.EventConfigMsg.Builder builder = MonopolyProto.EventConfigMsg.newBuilder();
            builder.setEventId(config.getEventId());
            builder.setEventType(config.getEventType().getValue());
            builder.setName(ServerLanguageMgr.getContent(config.getEventName(), language));
            builder.setDesc(ServerLanguageMgr.getContent(config.getDesc(), language));
            builder.setBubble(ServerLanguageMgr.getContent(config.getBubble(), language));
            builder.setAppearance(config.getAppearance());
            builder.setReward(config.getRewards());
            builder.setParamList(StringUtils.listToString(config.getParamList(), "|"));
            builder.setUpgradeParamList(StringUtils.listToString(config.getUpgradeParamList(), "|"));
            builder.setSound(config.getSound());
            builder.setDubbing(config.getDubbing());
            list.add(builder.build());
        }
        return list;
    }

    private static List<MonopolyProto.BossConfigMsg> parseBossConfigMsgList(DFWConfig dfwConfig, String language) {
        List<MonopolyProto.BossConfigMsg> list = new ArrayList<>();
        Map<Integer, DFWBossConfig> configMap = dfwConfig.getBossConfigMap();
        for (DFWBossConfig config : configMap.values()) {
            MonopolyProto.BossConfigMsg.Builder builder = MonopolyProto.BossConfigMsg.newBuilder();
            builder.setBossType(config.getBossType());
            builder.setAppearance(config.getAppearance());
            builder.setTotalHp(config.getHp());
            builder.setKillReward(config.getRewards());
            builder.setSort(config.getSort());
            list.add(builder.build());
        }
        return list;
    }

    /**
     * 商会配置
     */
    private static MonopolyProto.UnionConfigMsg parseUnionConfigMsg(DFWConfig dfwConfig) {
        MonopolyProto.UnionConfigMsg.Builder builder = MonopolyProto.UnionConfigMsg.newBuilder();
        builder.setZaoYaoAddition(dfwConfig.getZaoYaoAddition());
        builder.setMaxZaoYaoAddition(dfwConfig.getUnionMinAddition());
        builder.setZhuFuAddition(dfwConfig.getZhuFuAddition());
        builder.setMaxZhuFuAddition(dfwConfig.getUnionMaxAddition());
        builder.setBeatBackTimes(dfwConfig.getUnionBeatBackTimes());
        builder.setAdditionRecoverySecond(dfwConfig.getUnionAdditionRecoverySecond());
        builder.setZaoYaoNewsCount(dfwConfig.getZaoYaoNewsCount());
        builder.setZhuFuNewsCount(dfwConfig.getZhuFuAddition());
        builder.setEffectiveNewsCount(dfwConfig.getEffectiveNewsCount());
        builder.setUnEffectiveNewsCount(dfwConfig.getUnEffectiveNewsCount());
        return builder.build();
    }

    /**
     * 卡片配置
     */
    private static MonopolyProto.CardConfigMsg parseCardConfigMsg(DFWConfig dfwConfig) {
        MonopolyProto.CardConfigMsg.Builder builder = MonopolyProto.CardConfigMsg.newBuilder();
        builder.setCardIdList(dfwConfig.getCardIdList());
        builder.setRandomCardId(dfwConfig.getRandomCardId());
        return builder.build();
    }

    /**
     * 玩家配置
     */
    private static MonopolyProto.UserConfigMsg parseUserConfigMsg(DFWConfig dfwConfig) {
        MonopolyProto.UserConfigMsg.Builder builder = MonopolyProto.UserConfigMsg.newBuilder();
        builder.setMaxEnergy(dfwConfig.getMaxEnergy());
        builder.setAutoEventUnlockGold(dfwConfig.getAutoEventUnlockGold());
        builder.setEnergyRecoverySecond(dfwConfig.getEnergyRecoverySecond());
        builder.setEnergyItemId(dfwConfig.getEnergyItemId());
        builder.setTenRollUnlock(dfwConfig.getTenRollUnlock());
        return builder.build();
    }

    /**
     * 提取事件ID
     */
    private static List<Integer> parseEventIdList(List<Map<Integer, Integer>> configEventList) {
        List<Integer> idList = new ArrayList<>();
        for (Map<Integer, Integer> map : configEventList) {
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                idList.add(entry.getKey());
            }
        }
        return idList;
    }

    private static MonopolyProto.DollConfigMsg parseDollConfigMsg(DFWConfig config) {
        MonopolyProto.DollConfigMsg.Builder builder = MonopolyProto.DollConfigMsg.newBuilder();
        builder.setDollIdList(StringUtils.listToString(config.getDollIdList(), ";"));
        return builder.build();
    }

    public static MonopolyProto.UserDataMsg.Builder getUserDataMsg(DFWUserData userData) {
        MonopolyProto.UserDataMsg.Builder builder = MonopolyProto.UserDataMsg.newBuilder();
        builder.setGoldScore(userData.getGoldScore());
        builder.setYongBingTimes(userData.getYongBingTimes() - userData.getPreYongBingTimes());
        builder.setPositionIndex(userData.getPositionIndex());
        builder.setDollId(userData.getDollId());
        builder.setVehicleId(userData.getVehicleId());
        builder.setVehicleMoveTimes(userData.getVehicleMoveTimes());
        builder.setEnergy(userData.getEnergy());
        builder.setLastRecoveryTime(userData.getLastRecoveryTime());
        builder.setComplete(userData.isComplete());
        builder.setEventId(userData.getEventId());
        builder.setEventParam(userData.getExpandParam());
        builder.setFreeBattleTimes(userData.getFreeBattleTimes() - userData.getPreFreeBattleTimes());
        for (DFWDollData dfwDollData : userData.getDollList()) {
            builder.addDollList(getDollDataMsg(dfwDollData));
        }
        builder.setPositionData(parsePositionDataMsg(userData));
        for (DFWUserSecretRoad dfwUserSecretRoad : userData.getUserSecretRoadList()) {
            builder.addSecretData(parseSecretDataMsg(dfwUserSecretRoad));
        }
        for (DFWGodData dfwGodData : userData.getDfwGodDataList()) {
            builder.addGodData(parseGodData(dfwGodData));
        }
       // RankProto.RankListMsg.Builder rankMsg = CrossDataMgr.getCrossUserRankActivityRankMsg(DaFuWengMgr.getDfwConfig().getActivityInfo().getActivityId(), eGamePlayerEventType.DaFuWengScoreRank.getValue());
        //List<RankProto.RankTempMsg> rankList = rankMsg.getRankListList();
        int rank = -1;
        long userId = userData.getUserId();
        //OptionalInt result = IntStream.range(0, rankList.size()).filter(i -> rankList.get(i).getUserId() == userId).findFirst();
        /*if (result.isPresent()) {
            rank = result.getAsInt() + 1;
        }*/
        builder.setRank(rank);
        if (userData.getZaoYaoOptionalList() != null && !userData.getZaoYaoOptionalList().isEmpty()) {
            builder.addAllZaoYaoUnionList(parseMonopolyUnionMsgList(userData.getZaoYaoOptionalList()));
        }
        builder.addAllRebateRecord(userData.getRebateRewardRecord());
        for (Map.Entry<Integer, Integer> data : userData.getRewardCollectionMap().entrySet()) {
            MonopolyProto.ItemRewardTimesDataMsg.Builder builder1 = MonopolyProto.ItemRewardTimesDataMsg.newBuilder();
            builder1.setGoodsId(data.getKey());
            builder1.setTimes(data.getValue());
            builder.addItemRewardTimes(builder1);
        }
        for (DfwPackageData data : userData.getPackageList()) {
            MonopolyProto.DfwPackageMsg.Builder packageMsg = MonopolyProto.DfwPackageMsg.newBuilder();
            packageMsg.setMallId(data.getMallId());
            packageMsg.setNum(data.getNum());
            packageMsg.setType(data.getType());
            builder.addPackageList(packageMsg);
        }
        return builder;
    }

    public static List<MonopolyProto.MonopolyUnionMsg> parseMonopolyUnionMsgList(List<DFWZaoYaoOptional> zaoYaoOptionalList) {
        List<MonopolyProto.MonopolyUnionMsg> list = new ArrayList<>();
        for (DFWZaoYaoOptional optional : zaoYaoOptionalList) {
            MonopolyProto.MonopolyUnionMsg.Builder builder = MonopolyProto.MonopolyUnionMsg.newBuilder();
            builder.setUnionUid(optional.getUnionUid());
            builder.setRank(optional.getRank());
            builder.setSilverScore(optional.getScore());
            builder.setUnionData(UnionBasePb.parseUnionBaseTempMsg(optional.getBaseInfo()));
            list.add(builder.build());
        }
        return list;
    }

    public static List<DFWZaoYaoOptional> parseZaoYaoOptionalList(List<MonopolyProto.MonopolyUnionMsg> unionMsgList) {
        List<DFWZaoYaoOptional> list = new ArrayList<>();
        for (MonopolyProto.MonopolyUnionMsg optional : unionMsgList) {
            DFWZaoYaoOptional zaoYaoOptional = new DFWZaoYaoOptional();
            zaoYaoOptional.setUnionUid(optional.getUnionUid());
            zaoYaoOptional.setRank(optional.getRank());
            zaoYaoOptional.setScore(optional.getSilverScore());
            zaoYaoOptional.setBaseInfo(UnionBasePb.parseUnionBaseInfo(optional.getUnionData()));
            list.add(zaoYaoOptional);
        }
        return list;
    }

    public static MonopolyProto.UnionDataMsg.Builder getEmptyUnionDataMsg() {
        MonopolyProto.UnionDataMsg.Builder builder = MonopolyProto.UnionDataMsg.newBuilder();
        builder.setUnionAddition(0);
        builder.setIronCollectCount(0);
        builder.setOpenBoxCount(0);
        return builder;
    }

    public static MonopolyProto.SecretDataMsg.Builder parseSecretDataMsg(DFWUserSecretRoad secretRoad) {
        MonopolyProto.SecretDataMsg.Builder data = MonopolyProto.SecretDataMsg.newBuilder();
        data.setReward(secretRoad.getReward());
        data.setIsBigReward(secretRoad.isBigReward());
        data.setIsGetReward(secretRoad.isGetReward());
        data.setPositoonIndex(secretRoad.getPositionIndex());
        return data;
    }

    public static MonopolyProto.PositionDataMsg parsePositionDataMsg(DFWUserData userData) {
        MonopolyProto.PositionDataMsg.Builder builder = MonopolyProto.PositionDataMsg.newBuilder();
        for (LuZhangData luZhangData : userData.getLuZhangDataList()) {
            if (luZhangData.isSecretRoad()) {
                builder.addLuZhangOnSecret(luZhangData.getPositionIndex());
            } else {
                builder.addLuZhangList(luZhangData.getPositionIndex());
            }
        }
        return builder.build();
    }

    public static MonopolyProto.DollDataMsg.Builder getDollDataMsg(DFWDollData dfwDollData) {
        MonopolyProto.DollDataMsg.Builder builder = MonopolyProto.DollDataMsg.newBuilder();
        builder.setDollId(dfwDollData.getDollId());
        builder.setExpireTime(dfwDollData.getExpireTime());
        return builder;
    }

    /**
     * 用户信息
     */
    public static MonopolyProto.MonopolyPlayerMsg parseMonopolyPlayerMsg(long userId, PlayerProto.PlayerBaseTempMsg playerBaseTempMsg) {
        MonopolyProto.MonopolyPlayerMsg.Builder builder = MonopolyProto.MonopolyPlayerMsg.newBuilder();
        builder.setPlayer(playerBaseTempMsg);
        builder.setUserId(userId);
        return builder.build();
    }

    public static MonopolyProto.UnionMemberDollTemp.Builder parseUnionMemberDollTemp(DFWUserData dfwUserData, UserBaseInfo userBaseInfo) {
        MonopolyProto.UnionMemberDollTemp.Builder temp = MonopolyProto.UnionMemberDollTemp.newBuilder();
        temp.setUserId(dfwUserData.getUserId());
        temp.setUserName(userBaseInfo.getNickName());
        temp.setGoldScore(dfwUserData.getGoldScore());
        temp.setDollId(dfwUserData.getDollId());
        for (DFWGodData dfwGodData : dfwUserData.getDfwGodDataList()) {
            temp.addGodData(parseGodData(dfwGodData));
        }
        temp.setVehicleId(dfwUserData.getVehicleId());
        temp.setSex(userBaseInfo.getSex());
        MonopolyProto.UnionMemberMoveDataMsg.Builder moveData = MonopolyProto.UnionMemberMoveDataMsg.newBuilder();
        if (DaFuWengMgr.isSecretRoadEvent(dfwUserData)) {
            moveData.setSecretBeginPosition(Integer.parseInt(dfwUserData.getExpandParam()));
            moveData.setSecretOverPosition(Integer.parseInt(dfwUserData.getExpandParam()));
        }
        moveData.setNormalBeginPosition(dfwUserData.getPositionIndex());
        moveData.setNormalOverPosition(dfwUserData.getPositionIndex());
        moveData.setMoveStep(0);
        temp.setMoveData(moveData);
        return temp;
    }

    public static MonopolyProto.GodDataMsg.Builder parseGodData(DFWGodData dfwGodData) {
        MonopolyProto.GodDataMsg.Builder temp = MonopolyProto.GodDataMsg.newBuilder();
        temp.setGodId(dfwGodData.getGodId());
        temp.setGodMoveTimes(dfwGodData.getGodMoveTimes());
        return temp;
    }

}
